package cn.iocoder.yudao.module.ordering.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.ordering.controller.admin.vo.ThirdPartyMemberRespVO;
import cn.iocoder.yudao.module.ordering.convert.ThirdPartyMemberConvert;
import cn.iocoder.yudao.module.ordering.dal.dataobject.ThirdPartyMemberDO;
import cn.iocoder.yudao.module.ordering.service.ThirdPartyMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 第三方登录")
@RestController
@RequestMapping("/admin-api/ordering/third-party-login")
@Validated
@Slf4j
public class ThirdPartyLoginController {

    @Resource
    private ThirdPartyMemberService thirdPartyMemberService;

    @PostMapping("/login")
    @PermitAll
    @Operation(summary = "第三方登录", description = "通过第三方token进行登录，同步会员信息到系统")
    // @PreAuthorize("@ss.hasPermission('ordering:third-party-login:login')") // 暂时注释掉权限检查
    public CommonResult<ThirdPartyLoginRespVO> thirdPartyLogin(
            @Parameter(description = "第三方平台token", required = true) @RequestParam("token") String token) {
        
        log.info("收到第三方登录请求，token: {}", token);
        
        try {
            // 通过token同步第三方会员信息
            ThirdPartyMemberDO member = thirdPartyMemberService.syncSingleMemberByToken(token);
            
            if (member != null) {
                // 登录成功，返回会员信息
                ThirdPartyLoginRespVO loginResp = new ThirdPartyLoginRespVO();
                loginResp.setSuccess(true);
                loginResp.setMessage("登录成功");
                loginResp.setMember(ThirdPartyMemberConvert.INSTANCE.convert(member));
                loginResp.setToken("system_token_" + member.getUsername()); // 生成系统内部token
                
                log.info("第三方登录成功，用户名: {}", member.getUsername());
                return success(loginResp);
            } else {
                // 登录失败
                ThirdPartyLoginRespVO loginResp = new ThirdPartyLoginRespVO();
                loginResp.setSuccess(false);
                loginResp.setMessage("登录失败：无法获取第三方会员信息");
                
                log.warn("第三方登录失败，token: {}", token);
                return success(loginResp);
            }
        } catch (Exception e) {
            log.error("第三方登录异常", e);
            
            ThirdPartyLoginRespVO loginResp = new ThirdPartyLoginRespVO();
            loginResp.setSuccess(false);
            loginResp.setMessage("登录异常：" + e.getMessage());
            return success(loginResp);
        }
    }

    @GetMapping("/member-info")
    @PermitAll
    @Operation(summary = "获取第三方会员信息", description = "通过token获取第三方会员信息")
    // @PreAuthorize("@ss.hasPermission('ordering:third-party-login:query')") // 暂时注释掉权限检查
    public CommonResult<ThirdPartyMemberRespVO> getMemberInfo(
            @Parameter(description = "第三方平台token", required = true) @RequestParam("token") String token) {
        
        log.info("获取第三方会员信息，token: {}", token);
        
        ThirdPartyMemberDO member = thirdPartyMemberService.syncSingleMemberByToken(token);
        if (member != null) {
            log.info("获取第三方会员信息成功，用户名: {}", member.getUsername());
            return success(ThirdPartyMemberConvert.INSTANCE.convert(member));
        } else {
            log.warn("获取第三方会员信息失败，token: {}", token);
            return success(null);
        }
    }

    /**
     * 第三方登录响应VO
     */
    public static class ThirdPartyLoginRespVO {
        private Boolean success;
        private String message;
        private ThirdPartyMemberRespVO member;
        private String token;

        // Getters and Setters
        public Boolean getSuccess() { 
            return success; 
        }
        
        public void setSuccess(Boolean success) { 
            this.success = success; 
        }
        
        public String getMessage() { 
            return message; 
        }
        
        public void setMessage(String message) { 
            this.message = message; 
        }
        
        public ThirdPartyMemberRespVO getMember() { 
            return member; 
        }
        
        public void setMember(ThirdPartyMemberRespVO member) { 
            this.member = member; 
        }
        
        public String getToken() { 
            return token; 
        }
        
        public void setToken(String token) { 
            this.token = token; 
        }
    }

}
